package com.giser.testapp.lib.Location;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationClientOption.AMapLocationProtocol;
import com.amap.api.location.AMapLocationListener;
import com.giser.testapp.MainActivity;
import com.giser.testapp.lib.App.EnvApplication;
import com.giser.testapp.lib.Tool.EnvLog;

import rx.Observable;
import rx.Observer;

/**
 * Created by Lss on 2017/12/9.
 */

public class AMapLocationServer implements AMapLocationListener {
    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    private boolean isStart=false;
    public AMapLocationServer(){
        initLocation();
    }
    /**
     * 初始化定位
     */
    private void initLocation(){
        //初始化client
        locationClient = new AMapLocationClient(EnvApplication.getApplication().getApplicationContext());
        locationOption = getDefaultOption();
        //设置定位参数
        locationClient.setLocationOption(locationOption);
        // 设置定位监听
        locationClient.setLocationListener(this);
    }
    /**
     * 默认的定位参数
     */
    private AMapLocationClientOption getDefaultOption(){
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(2000);//可选，设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(false);//可选，设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mOption.setLocationCacheEnable(true); //可选，设置是否使用缓存定位，默认为true
        return mOption;
    }

    /**
     * 开始定位
     */
    public void startLocation(){
        isStart=true;
        // 启动定位
        if(locationClient==null){
            EnvLog.i("locationClient==null");
            initLocation();
        }
        locationClient.startLocation();
    }

    /**
     * 停止定位
     */
    public void stopLocation(){
        isStart=false;
        // 停止定位
        if(locationClient!=null){
            locationClient.stopLocation();
        }
    }

    /**
     * 销毁定位
     */
    public void destroyLocation(){
        EnvLog.i("amp destroyLocation....");
        if (null != locationClient) {
            /**
             * 如果AMapLocationClient是在当前Activity实例化的，
             * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
             */
            locationClient.onDestroy();
            locationClient = null;
            locationOption = null;
        }
    }

    /**
     * 定位监听
     */
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        EnvApplication.getApplication().geographicLocation=LocationConverter.aMapToGeographicLocation(aMapLocation,EnvApplication.getApplication().geographicLocation);
       // EnvLog.i("df="+EnvApplication.getApplication().geographicLocation);
        if(EnvApplication.getApplication().firstChangeModue==1){
            Observable<GeographicLocation> loc = Observable.just(EnvApplication.getApplication().geographicLocation);
            loc.subscribe(new Observer<GeographicLocation>() {
                @Override
                public void onCompleted() { }

                @Override
                public void onError(Throwable e) { }

                @Override
                public void onNext(GeographicLocation locs) {
                    EnvApplication.getApplication().getLocationServer((MainActivity)EnvApplication.getApplication().minContext).convertGeographicLocation(locs);
                    EnvApplication.getApplication().firstChangeModue=0;
                }
            });
        }
    }
    public void setStart(boolean tag){
        this.isStart=tag;
    }
    public  boolean getIsStart(){
        return isStart;
    }
}
